package com.zuo.mall.controller;

import com.zuo.mall.common.api.CommonPage;
import com.zuo.mall.common.api.CommonResult;
import com.zuo.mall.entity.OmsOrderReturnReason;
import com.zuo.mall.service.OmsOrderReturnReasonService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Api("退款原因设置")
@RestController()
@RequestMapping("returnReason")
public class OmsOrderReturnReasonController {

    @Autowired
    private OmsOrderReturnReasonService orderReturnReasonService;

    @GetMapping("list")
    public CommonResult<CommonPage<OmsOrderReturnReason>> list(
            @RequestParam(value = "pageNum") Integer pageNum,
            @RequestParam(value = "pageSize") Integer pageSize
    ) {
        CommonPage<OmsOrderReturnReason> reasonList = orderReturnReasonService.list(pageNum, pageSize);
        return CommonResult.success(reasonList);
    }

    @GetMapping("{id}")
    public CommonResult<OmsOrderReturnReason> getItem(@PathVariable("id") Long id) {
        OmsOrderReturnReason byId = orderReturnReasonService.getById(id);
        return CommonResult.success(byId);
    }

    @PostMapping("update/{id}")
    public CommonResult update(@PathVariable("id") Long id, @RequestBody OmsOrderReturnReason orderReturnReason) {
        orderReturnReason.setId(id);
        boolean b = orderReturnReasonService.updateById(orderReturnReason);
        if (b) {
            return CommonResult.success(1);
        }
        return CommonResult.failed();
    }

    @PostMapping("update/status")
    public CommonResult updateStatus(@RequestParam("status") Integer status, @RequestParam("ids") List<Long> ids) {
        int count = orderReturnReasonService.updateStatusBatch(ids, status);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @PostMapping("create")
    public CommonResult createStatus(@RequestBody OmsOrderReturnReason omsOrderReturnReason) {
        omsOrderReturnReason.setCreateTime(LocalDateTime.now());
        boolean save = orderReturnReasonService.save(omsOrderReturnReason);
        if (save) {
            return CommonResult.success(1);
        }
        return CommonResult.failed();
    }

    @PostMapping("delete")
    public CommonResult delete(@RequestParam List<Long> ids) {
        boolean del = orderReturnReasonService.removeByIds(ids);
        if (del) {
            return CommonResult.success(del);
        }
        return CommonResult.failed();
    }
}
